<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\RoleRequest;
use App\Http\Resources\RoleCollectionResource;
use App\Http\Resources\RoleResource;
use App\Models\Menu;
use App\Models\Permission;
use Illuminate\Http\Request;
use App\Models\Role;

class RoleController extends Controller
{
    /**
     * 角色列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $params = $request->all();

        $query = Role::with(['permissions' => function ($query) {
            $query->select('permissions.id');
        }, 'menus' => function ($query) {
            $query->select('menus.id');
        }]);

        if ($name = $params['name'] ?? '') {
            $like = '%' . $name . '%';
            $query->where('name', 'like', $like)->orWhere('label', 'like', $like);
        }
        $roles = $query->get();

        return $this->success(new RoleCollectionResource($roles));
    }

    /**
     * 角色详情
     * @param Role $role
     * @return mixed
     */
    public function show(Role $role)
    {
        return $this->success(new RoleResource($role));
    }

    /**
     * 新增角色
     * @param RoleRequest $request
     * @return mixed
     */
    public function store(RoleRequest $request)
    {
        Role::create($request->only(['name', 'label', 'description']));

        return $this->created('角色新增成功');
    }

    /**
     * 更新角色
     * @param RoleRequest $request
     * @param Role $role
     * @return mixed
     */
    public function update(RoleRequest $request, Role $role)
    {
        $role->fill($request->only(['name', 'label', 'description']));
        $role->save();

        return $this->success(new RoleResource($role));
    }

    /**
     * 获取基础信息
     * @return mixed
     */
    public function params()
    {
        $permissions = Permission::with('children')->where('pid', 0)->get();
        $menus = Menu::with('children')->where('pid', 0)->get();

        return $this->success(compact('permissions', 'menus'));
    }

    /**
     * 给角色分配权限
     * @param RoleRequest $request
     * @param Role $role
     * @return mixed
     */
    public function permissions(RoleRequest $request, Role $role)
    {
        $ids = $request->input('ids', []);
        $permissions = Permission::whereIn('id', $ids)->get();
        $role->syncPermissions($permissions);

        return $this->success('权限分配成功');
    }

    /**
     * 给角色分配菜单
     * @param RoleRequest $request
     * @param Role $role
     * @return mixed
     */
    public function menus(RoleRequest $request, Role $role)
    {
        $ids = $request->input('ids', []);
        $menus = Menu::whereIn('id', $ids)->pluck('id')->toArray();
        $role->menus()->sync($menus);

        return $this->success('菜单分配成功');
    }

    /**
     * 删除角色
     * @param Role $role
     * @return mixed
     */
    public function destroy(Role $role)
    {
        try {
            if ($role->users()->exists()) {
                return $this->failed('请先删除该角色下的用户');
            }
            if ($role->permissions()->exists()) {
                return $this->failed('请先删除该角色下的权限');
            }
            if ($role->menus()->exists()) {
                return $this->failed('请先删除该角色下的的菜单');
            }
            $role->delete();
            return $this->success('角色删除成功');
        } catch (\Exception $exception) {
            return $this->failed($exception->getMessage());
        }
    }
}
